package com.gin.no_gay_alliance.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.webkit.CookieManager;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.gin.nga.client.NgaClient;
import com.gin.no_gay_alliance.databinding.ActivityLoginBinding;

/**
 * 登陆活动
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/22 11:37
 **/
public class LoginActivity extends BaseBindingActivity<ActivityLoginBinding> {
    public static final String TAG = LoginActivity.class.getSimpleName();
    public static final String LOGIN_URL = "https://bbs.nga.cn/nuke.php?__lib=login&__act=account&login";
    public static final String COOKIE_KEY = "cookie";
    WebViewClient webViewClient;
    WebChromeClient webChromeClient;

    @Override
    protected void onPause() {
        setCookie(getCookie());
        super.onPause();
        b.wv.onPause();
    }

    /**
     * 解析意图
     *
     * @param intent 意图
     */
    @Override
    protected void handleIntent(Intent intent) {

    }

    @Override
    @SuppressLint("SetJavaScriptEnabled")
    protected void onCreate() {
        webViewClient = new WebViewClient() {
            //设置在webView点击打开的新网页在当前界面显示,而不跳转到新的浏览器中
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                String url = request.getUrl().toString();
                Log.d(TAG, "shouldOverrideUrlLoading: " + url);
                view.loadUrl(url);
                return false;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                CookieManager.getInstance().removeAllCookies(null);
                CookieManager.getInstance().flush();
            }
        };

        webChromeClient = new WebChromeClient() {
            @Override
            public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
                Log.d(TAG, "onJsConfirm: " + url + " msg:" + message);
                String cookie = getCookie();
                if (NgaClient.isCookieLegal(cookie)) {
                    // 合法cookie 返回
                    setCookie(NgaClient.decodeUsername(cookie));
                    finish();
                    return true;
                }
                return super.onJsConfirm(view, url, message, result);
            }
        };


        b.wv.clearCache(true);
        b.wv.clearHistory();
        b.wv.setWebViewClient(webViewClient);
        b.wv.setWebChromeClient(webChromeClient);
        //设置WebView属性,运行执行js脚本
        b.wv.getSettings().setJavaScriptEnabled(true);
        b.wv.loadUrl(LOGIN_URL);
    }

    /**
     * 装载右上角菜单
     *
     * @param menu         菜单
     * @param menuInflater 菜单渲染器
     */
    @Override
    protected void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {

    }

    @Override
    protected void onDestroy() {
        b.wv.destroy();
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        b.wv.onResume();
        super.onResume();
    }

    private String getCookie() {
        CookieManager cookieManager = CookieManager.getInstance();
        return cookieManager.getCookie(b.wv.getUrl()) + ";";
    }

    private void setCookie(String cookie) {
        Intent intent = new Intent();
        intent.putExtra(COOKIE_KEY, cookie);
        setResult(200, intent);
    }
}
